package tests;

import common.Utils;
import org.checkerframework.checker.units.qual.A;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.devtools.v85.io.IO;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;


public class LoginPage extends Utils {
    private static String url = "http://120.26.87.94:8080/blog_login.html";
    public LoginPage() {
        super(url);
    }

    public void LoginPageRight(){
        driver.get(url);
        //通过查看页面元素判断是否加载完成
        driver.findElement(By.cssSelector("body > div.nav"));
        driver.findElement(By.cssSelector("body > div.container-login > div > h3"));
        driver.findElement(By.cssSelector("body > div.container-login > div"));
    }

    /**
     * 登陆成功
     */
    public void LoginSuccess() throws IOException {
        //清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();

        //输入正确的用户名和密码
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //查看注销按钮
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
        getScreenShot(getClass().getName());
        //查看url
        String expect = driver.getTitle();

        assert expect.equals("博客列表页");

//        driver.navigate().back();


    }

    public void LoginFail() throws IOException, UnhandledAlertException, InterruptedException {
//        //方法一：通过clear保证输入框没有文本
//        driver.findElement(By.cssSelector("#username")).clear();
//        driver.findElement(By.cssSelector("#password")).clear();

        //方法二：通过刷新保证输入框没有文本
        driver.navigate().refresh();

        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");


        //
        //


        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(2000);
        String alertText = "";


        Alert alert = driver.switchTo().alert();


        alertText = alert.getText();
        Thread.sleep(2000);
        alert.accept();
        getScreenShot(getClass().getName());



        assert alertText.equals("密码错误");
        driver.quit();
    }
}
